package com.todo.member.infrastructure.repository.impl;

import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.utils.StringUtils;
import com.todo.member.common.enums.LevelStatusEnum;
import com.todo.member.infrastructure.convert.MemberBaseConvert;
import com.todo.member.infrastructure.entity.condition.MemberLevelInfoCondition;
import com.todo.member.infrastructure.entity.po.MemberLevelInfoPO;
import com.todo.member.infrastructure.mapper.MemberLevelInfoMapper;
import com.todo.member.infrastructure.repository.MemberLevelInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author azhebuxing
 * @date 2025/2/11 11:27
 * @description
 */
@Slf4j
@Repository
public class MemberLevelInfoRepositoryImpl implements MemberLevelInfoRepository {

    @Resource
    private MemberLevelInfoMapper memberLevelInfoMapper;

    @Override
    public Boolean insertMemberLevelInfoPO(MemberLevelInfoPO memberLevelInfoPO) {
        if (memberLevelInfoPO == null) {
            return false;
        }
        if (memberLevelInfoPO.getLevelName() == null || memberLevelInfoPO.getLevelCode() == null) {
            return false;
        }
        return memberLevelInfoMapper.insertMemberLevelInfoPO(memberLevelInfoPO) > 0;
    }

    @Override
    public Boolean updateMemberLevelInfoPOById(MemberLevelInfoPO memberLevelInfoPO) {
        if (memberLevelInfoPO == null || memberLevelInfoPO.getId() == null) {
            return false;
        }
        //是否需要将操作代码放入此处，例如version+1，替换id等
        MemberLevelInfoPO updateData = MemberBaseConvert.buildUpdateMemberLevelInfo(memberLevelInfoPO);
        return insertMemberLevelInfoPO(updateData);
    }

    @Override
    public Boolean removeMemberLevelInfoPOById(Long id) {
        if (id == null || id < 0) {
            return false;
        }
        return memberLevelInfoMapper.removeMemberLevelInfoPOById(id) > 0;
    }

    @Override
    public MemberLevelInfoPO queryMemberLevelInfoPOById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        return memberLevelInfoMapper.queryMemberLevelInfoPOById(id);
    }

    @Override
    public MemberLevelInfoPO queryMemberLevelInfoPOByLevelCode(String levelCode) {
        if (StringUtils.isBlank(levelCode)) {
            return null;
        }
        return memberLevelInfoMapper.queryMemberLevelInfoPOByLevelCode(levelCode);
    }

    @Override
    public MemberLevelInfoPO queryMemberLevelInfoPOByName(String name) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        return memberLevelInfoMapper.queryMemberLevelInfoPOByName(name);
    }

    @Override
    public Boolean updateMemberLevelInfoStatusById(Long id, LevelStatusEnum oldStatus, LevelStatusEnum newStatus) {
        if (id == null || id < 0 || oldStatus == null || newStatus == null) {
            return false;
        }
        return memberLevelInfoMapper.updateMemberLevelInfoStatusById(id, oldStatus.getType(), newStatus.getType());
    }

    @Override
    public PageResult<MemberLevelInfoPO> queryMemberLevelInfoPOByPage(MemberLevelInfoCondition memberLevelInfoCondition) {
        if (memberLevelInfoCondition == null) {
            return PageResult.of(new ArrayList<>(), 0, 0, 0);
        }
        Integer count = memberLevelInfoMapper.queryCountByCondition(memberLevelInfoCondition);
        if (count == 0) {
            return PageResult.of(new ArrayList<>(), memberLevelInfoCondition.getPageIndex(), memberLevelInfoCondition.getPageSize(), 0);
        }
        List<MemberLevelInfoPO> memberLevelInfoPOS = memberLevelInfoMapper.queryMemberLevelInfoPOCondition(memberLevelInfoCondition);
        PageResult<MemberLevelInfoPO> pageResult = PageResult.of(memberLevelInfoPOS, memberLevelInfoCondition.getPageIndex(), memberLevelInfoCondition.getPageSize(), count);
        return pageResult;
    }

    @Override
    public MemberLevelInfoPO queryMemberLevelByName(String levelName) {
        if (StringUtils.isBlank(levelName)) {
            return null;
        }
        MemberLevelInfoPO memberLevelInfoPO = memberLevelInfoMapper.queryMemberLevelByName(levelName);
        return memberLevelInfoPO;
    }
}
